package com.itany.corejava.day14;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class FileSplitter {

    private JFrame jf;
    private JLabel jl1;
    private JLabel jl2;
    private JTextField jtf1;
    private JTextField jtf2;
    private JButton jb1;
    private JButton jb2;
    private JButton jb3;

    public FileSplitter() {
        jf=new JFrame("文件分割器");
        jl1=new JLabel("文件：");
        jl2=new JLabel("大小：");
        jtf1=new JTextField(20);
        jtf2=new JTextField(5);
        jb1=new JButton("打开");
        jb2=new JButton("分割");
        jb3=new JButton("合并");

    }

    private void init(){
        JPanel jp1=new JPanel();
        jp1.add(jl1);
        jp1.add(jtf1);
        jp1.add(jb1);

        JPanel jp2=new JPanel();
        jp2.add(jl2);
        jp2.add(jtf2);
        jp2.add(jb2);
        jp2.add(jb3);

        jf.add(jp1, BorderLayout.NORTH);
        jf.add(jp2);


    }

    private void addEventHandler(){
        jb1.addActionListener(e->{

            FileDialog fd=new FileDialog(jf,"打开文件",FileDialog.LOAD);

            fd.setVisible(true);

            if(fd.getFile()!=null){
                jtf1.setText(fd.getDirectory()+fd.getFile());
            }


        });

        jb2.addActionListener(e->{
            if("".equals(jtf1.getText())||"".equals(jtf2.getText())){
                JOptionPane.showMessageDialog(jf,"信息不完整");
                return;
            }


            try(FileInputStream fis=new FileInputStream(jtf1.getText())){

                int n;

                byte[] b=new byte[Integer.parseInt(jtf2.getText())];

                int i=1;

                while((n=fis.read(b))!=-1){
                    try(FileOutputStream fos=new FileOutputStream(jtf1.getText()+(i++))) {
                        fos.write(b,0,n);
                    }catch (Exception ex){
                        ex.printStackTrace();
                        throw ex;
//                        throw new Exception(ex);
                    }
                }




                JOptionPane.showMessageDialog(jf,"分割成功");
            }catch(FileNotFoundException ex){
                JOptionPane.showMessageDialog(jf,"选择的文件不存在");
            }catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(jf,"大小必须是数字");
            }catch (Exception ex){
                ex.printStackTrace();
                JOptionPane.showMessageDialog(jf,"分割失败");
            }
        });

        jb3.addActionListener(e->{
            if("".equals(jtf1.getText())){
                JOptionPane.showMessageDialog(jf,"信息不完整");
                return;
            }

            try(FileOutputStream fos=new FileOutputStream(jtf1.getText())){
                int i=1;
                while(true){

                    File file=new File(jtf1.getText()+(i++));

                    if(file.exists()==false){
                        break;
                    }

                    try(FileInputStream fis=new FileInputStream(file)){

                        byte[] b=new byte[fis.available()];

                        int n=fis.read(b);

                        fos.write(b,0,n);

                    }catch (Exception ex){
                        ex.printStackTrace();
                        throw ex;
                    }


                }

                JOptionPane.showMessageDialog(jf,"合并成功");
            }catch (Exception ex){
                ex.printStackTrace();
                JOptionPane.showMessageDialog(jf,"合并失败");
            }




        });




    }

    public void show(){
        init();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new FileSplitter().show();
    }

















}
